
//
//  ForumContentViewController.m
//  cityCar
//
//  Created by yao on 14-10-31.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ForumContentViewController.h"
#import "Connection.h"
#import "WebColor.h"
#import "MJRefresh.h"
#import "DataBaseHandler.h"
#import "Forum.h"

@interface ForumContentViewController ()<UIWebViewDelegate>

@property(nonatomic, retain)UIWebView *myWeb;

// 收藏的label
@property(nonatomic, retain)UILabel *collectLabel;
// 收藏按钮
@property(nonatomic, retain)UIButton *bottomButton;
// loading
@property(nonatomic, retain)LoadingView *load;

@end

@implementation ForumContentViewController

- (void)dealloc
{
    [_load release];
    [_forum release];
    [_stringId release];
    [_myWeb release];
    [_collectLabel release];
    [_bottomButton release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.countx = 1;
        self.forum = [[[Forum alloc]init] autorelease];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.automaticallyAdjustsScrollViewInsets = NO;

    [self startConnection];
    [self setupView];
    self.load= [[LoadingView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_load];
    [self.view bringSubviewToFront:_load];
    [_load release];
    
    // 监听网络状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    Reachability *hostReach = [Reachability reachabilityWithHostName:@"www.google.com"];//可以以多种形式初始化
    [hostReach startNotifier];
    
}
#pragma mark 判断网络状态
// 连接改变
- (void) reachabilityChanged: (NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self updateInterfaceWithReachability: curReach];
}

//处理连接改变后的情况
- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    //对连接改变做出响应的处理动作。
    NetworkStatus status = [curReach currentReachabilityStatus];
    
    if (status != NotReachable) {
        NSURL *url = [NSURL URLWithString:self.forum.url];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [self.myWeb loadRequest:request];
    }
    
}

#pragma mark -

- (void)setupView
{
    // 视图控制器的宽和高
    CGFloat x = self.view.frame.size.width;
    CGFloat y = self.view.frame.size.height;
    
    self.view.backgroundColor = [WebColor white];
    
    // 在顶部加一个View
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, x, 64)];
    headView.backgroundColor = [WebColor whiteSmoke];
    [self.view addSubview:headView];
    [headView release];
    
    // 在顶部View上加一个label
    UILabel *headLabel = [[UILabel alloc] initWithFrame:CGRectMake((x - 100) / 2, 15, 100, 50)];
    headLabel.text = @"详情";
    headLabel.font = [UIFont fontWithName:@"Helvetica" size:20];
    headLabel.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:headLabel];
    [headLabel release];
    
    // 在底部加一个bottomView
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, y - 49, x, 49)];
    bottomView.backgroundColor = [WebColor whiteSmoke];
    [self.view addSubview:bottomView];
    [bottomView release];
    
    // 在bottomView上加收藏的button
    self.bottomButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.bottomButton.frame = CGRectMake(x - 40, 25, 30, 30);
    [self.bottomButton setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-3.png"] forState:UIControlStateNormal];
    [self.bottomButton addTarget:self action:@selector(collectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:self.bottomButton];
    
    // 后退
    UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    back.tag = 9546;
    [back setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(forwardAndBackAction:) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(20, 7, 30, 30);
    [bottomView addSubview:back];
    
    // 前进
    UIButton *forward = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [forward setBackgroundImage:[UIImage imageNamed:@"forward.png"] forState:UIControlStateNormal];
    [forward addTarget:self action:@selector(forwardAndBackAction:) forControlEvents:UIControlEventTouchUpInside];
    forward.frame = CGRectMake(self.view.frame.size.width - 50, 7, 30, 30);
    [bottomView addSubview:forward];

    
    // 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 25, 30, 30);
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(swipeAction:) forControlEvents:UIControlEventTouchUpInside];
    backButton.tintColor = [WebColor black];
    [headView addSubview:backButton];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
    [swipe release];
    
    
    // 显示收藏的label
    self.collectLabel = [[UILabel alloc] initWithFrame:CGRectMake((x - 100) / 2, 200, 100, 30)];
    self.collectLabel.hidden = YES;
    self.collectLabel.textColor = [WebColor white];
    self.collectLabel.alpha = 0.5;
    self.collectLabel.backgroundColor = [WebColor black];
    self.collectLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.collectLabel];
    [_collectLabel release];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(cartoon) userInfo:nil repeats:YES];
    
}

// 每次进入页面都执行的方法
- (void)viewDidAppear:(BOOL)animated
{
    [self settingButton];
}

- (void)startConnection
{
    
    self.myWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height + 130)];
    [self.view sendSubviewToBack:self.myWeb];
    self.myWeb.scrollView.showsVerticalScrollIndicator = NO;
    self.myWeb.scrollView.bounces = NO;
    self.myWeb.delegate = self;
    NSURL *url = [NSURL URLWithString:self.forum.url];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [self.myWeb loadRequest:request];
    [self.view addSubview:self.myWeb];
    // 在webView加在self.view之后设置,目的让webView始终在最后面
    [self.view sendSubviewToBack:self.myWeb];
    [_myWeb release];

}
#pragma mark - webView协议

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_load removeFromSuperview];
}

#pragma mark -

// 前进后退方法
- (void)forwardAndBackAction:(UIButton *)Button
{
    
    switch (Button.tag) {
        case 9546:
        {
            [_myWeb goBack];
        }
            break;
        default:
        {
            [_myWeb goForward];
        }
            break;
    }
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    // 返回上一页面
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 点击收藏
- (void)collectButtonClicked:(UIButton *)button
{
    DataBaseHandler *handler = [DataBaseHandler shareInstrance];
    
    UIImage *image = [UIImage imageNamed:@"iconfont-shoucang-3.png"];
    UIImage *buttonImage = [button backgroundImageForState:UIControlStateNormal];
    if ([buttonImage isEqual:image]) {
        [button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-4.png"] forState:UIControlStateNormal];
        self.collectLabel.hidden = NO;
        self.collectLabel.text = @"收藏成功";
        [self.view bringSubviewToFront:self.collectLabel];
        
        // 创建表
        [handler createTableWeb];
        
        // model类
        ForumGather *forum = [[ForumGather alloc] init];
        forum.name = self.forum.title;
        forum.Id = self.forum.topicId;
        forum.image = self.forum.image;
        forum.url = self.forum.url;
        NSLog(@"wang址： %@", forum.url);
        
        // 添加表
        [handler insertWeb:forum];
        [forum release];
        
        
    } else {
        [button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-3.png"] forState:UIControlStateNormal];
        self.collectLabel.hidden = NO;
        self.collectLabel.text = @"取消收藏";
        [self.view bringSubviewToFront:self.collectLabel];
        [handler deleteWeb:self.forum.topicId];
    }
}

// 设置button的背景色
- (void)settingButton
{
    DataBaseHandler *handler = [DataBaseHandler shareInstrance];
    NSString *string = [NSString stringWithFormat:@"%@", self.forum.topicId];
    for (ForumGather *fav in [handler selectWebAll]) {
        if ([string isEqualToString:fav.Id]) {
            [self.bottomButton setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-4.png"] forState:UIControlStateNormal];
            break;
        } else {
            [self.bottomButton setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-3.png"] forState:UIControlStateNormal];
        }
    }
}


// 收藏自动取消
- (void)cartoon
{
    self.collectLabel.hidden = YES;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
